package com.shtel.paas.monitorcenter.config.management.whiteListConfig.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shtel.paas.monitorcenter.config.management.whiteListConfig.entity.WhiteListEntity;
import com.shtel.paas.monitorcenter.config.management.whiteListConfig.entity.WhiteListHostIdDto;
import com.shtel.paas.monitorcenter.config.management.whiteListConfig.entity.WhiteListResponseDto;
import com.shtel.paas.monitorcenter.config.management.whiteListConfig.entity.WhiteListRulesDto;
import com.shtel.paas.monitorcenter.config.management.whiteListConfig.mapper.WhiteListConfigMapper;
import com.shtel.paas.monitorcenter.config.management.whiteListConfig.service.IWhiteListService;

import java.util.Date;
import java.util.List;
@Service
public class WhiteListService implements IWhiteListService{

	@Autowired
	WhiteListConfigMapper dao;
	
	@Override
	public PageInfo<WhiteListResponseDto> selectAll(String hostId, String ruleId, Integer page, Integer pageSize) {
		PageHelper.startPage(page, pageSize);
		List<WhiteListResponseDto> list = dao.selectAll(hostId, ruleId);
		PageInfo<WhiteListResponseDto> result = new PageInfo(list);
		for(WhiteListResponseDto dto : list){
			if(dto.getUser().equals("1")){
				dto.setUser("系统用户");
			}
		}
		result.setList(list);
		return result;
	}

	@Override
	public List<WhiteListHostIdDto> selectHostId() {
		
		return dao.selectHostId();
	}

	@Override
	public List<WhiteListRulesDto> selectRules() {

		return dao.selectRules();
	}

	@Override
	public int insertWhiteList(String host, String rules) {
		String[] ss =new String[200];
		ss=host.split("-");
		String hostId=ss[0];
		String ip = null;
		if(ss.length>1){
		 ip =ss[1];
		}
		WhiteListEntity entity = new WhiteListEntity();
		entity.setCreateDate(new Date());
		entity.setHostId(hostId);
		entity.setUpdateUser("1");
		entity.setWhiteIp(ip);
		entity.setWhiteRuleId(rules);
		dao.insertWhiteList(entity);
		return 0;
	}

	/**
	 * 删除白名单配置方法
	 * @param:  主键id
	 * @author: zhangyy
	 * @Date:   2018.05.16
	 * @return: boolean
	 */
	@Override
	public boolean  delectWhiteList(String id) {
		// TODO Auto-generated method stub
		try{
			if(dao.delectWhiteList(id)) {
				return true;
			}
		}catch(Exception e){
		    throw e; 
		}
		return false;
	}

}
